package com.ruoyi.user.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.user.domain.vo.CartItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.user.mapper.UserShoppingCartMapper;
import com.ruoyi.user.domain.UserShoppingCart;
import com.ruoyi.user.service.IUserShoppingCartService;

/**
 * 用户购物车Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@Service
public class UserShoppingCartServiceImpl implements IUserShoppingCartService
{
    @Autowired
    private UserShoppingCartMapper userShoppingCartMapper;

    /**
     * 查询用户购物车
     *
     * @param cartId 用户购物车主键
     * @return 用户购物车
     */
    @Override
    public UserShoppingCart selectUserShoppingCartByCartId(String cartId)
    {
        return userShoppingCartMapper.selectUserShoppingCartByCartId(cartId);
    }

    /**
     * 查询用户购物车列表
     *
     * @param userShoppingCart 用户购物车
     * @return 用户购物车
     */
    @Override
    public List<UserShoppingCart> selectUserShoppingCartList(UserShoppingCart userShoppingCart)
    {
        return userShoppingCartMapper.selectUserShoppingCartList(userShoppingCart);
    }

    /**
     * 新增用户购物车
     *
     * @param userShoppingCart 用户购物车
     * @return 结果
     */
    @Override
    public int insertUserShoppingCart(UserShoppingCart userShoppingCart)
    {
        return userShoppingCartMapper.insertUserShoppingCart(userShoppingCart);
    }

    /**
     * 修改用户购物车
     *
     * @param userShoppingCart 用户购物车
     * @return 结果
     */
    @Override
    public int updateUserShoppingCart(UserShoppingCart userShoppingCart)
    {
        return userShoppingCartMapper.updateUserShoppingCart(userShoppingCart);
    }

    /**
     * 批量删除用户购物车
     *
     * @param cartIds 需要删除的用户购物车主键
     * @return 结果
     */
    @Override
    public int deleteUserShoppingCartByCartIds(String[] cartIds)
    {
        return userShoppingCartMapper.deleteUserShoppingCartByCartIds(cartIds);
    }

    /**
     * 删除用户购物车信息
     *
     * @param (merchantId customerId) 用户购物车主键
     * @return 结果
     */
    @Override
    public int deleteUserShoppingCartByCartId(Long customer ,Long merchantId)
    {
        return userShoppingCartMapper.deleteUserShoppingCartByCartId(customer,merchantId);
    }

    @Override
    public List<CartItemVO> listCartItems(Long customerId, Long merchantId) {
        return userShoppingCartMapper.selectCartItemsByCustomerAndMerchant(customerId, merchantId);
    }

    @Override
    public BigDecimal getCartTotalPrice(Long customerId, Long merchantId) {
        return userShoppingCartMapper.selectCartTotalPrice(customerId, merchantId);
    }

    @Override
    public void incrementItem(Long customerId, Long merchantId, Long productId) {
        userShoppingCartMapper.incrementCartItem(customerId,merchantId, productId);
    }

    @Override
    public void decrementItemOrDelete(Long customerId, Long merchantId, Long productId) {
        int updated = userShoppingCartMapper.decrementCartItem(customerId,merchantId, productId);
        if (updated == 0) {
            userShoppingCartMapper.deleteUserShoppingCartByCartId(customerId,merchantId);
        }
    }

    @Override
    public int insertCartItemByKeys(Long customerId, Long merchantId, Long productId) {
        return userShoppingCartMapper.insertCartItemByKeys(customerId, merchantId, productId);
    }
}
